package com.cda.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cda.mall.dao.UsersMapper;
import com.cda.mall.entity.Users;
import com.cda.mall.service.IUsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cda.mall.utils.MD5Utils;
import com.cda.mall.vo.ResStatus;
import com.cda.mall.vo.ResultVO;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.impl.DefaultJwtBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;


/**
 * <p>
 * 用户  服务实现类
 * </p>
 *
 * @author cda
 * @since 2021-11-13
 */
@Service
//@Scope("singleton") //单例模式，只有一个Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {

    @Autowired
    private UsersMapper usersMapper;

    public ResultVO userRegister(String name, String pwd) {
        synchronized (this){
            //1.根据用户查询，这个用户是否已经被注册
            QueryWrapper<Users> wrapper = new QueryWrapper<>();
            wrapper.eq("username",name);
            Users one = usersMapper.selectOne(wrapper);
            //2.如果没有被注册则进行保存操作
            if (one == null){
                String md5Pwd = MD5Utils.md5(pwd);
                Users user = new Users();
                user.setUsername(name);
                user.setPassword(md5Pwd);
                user.setUserImg("aaa/img");
                user.setUserRegtime(new Date());
                user.setUserModtime(new Date());
                int insert = usersMapper.insert(user);
                if (insert > 0){
                    return new ResultVO(ResStatus.OK,"注册成功！",null);
                }else {
                    return new ResultVO(ResStatus.NO,"注册失败！",null);
                }
            }else {
                return new ResultVO(ResStatus.NO,"用户名已经被注册",null);
            }
        }
    }

    @Override
    public ResultVO checkLogin(String name, String pwd) {
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("username",name);
        Users one = usersMapper.selectOne(wrapper);
        if (one == null){
            return new ResultVO(ResStatus.NO,"登录失败，用户名不存在",null);
        }else {
            String md5Pwd = MD5Utils.md5(pwd);
            if (md5Pwd.equals(one.getPassword())){

                JwtBuilder builder = Jwts.builder();

                HashMap<String ,Object> map = new HashMap<>();
                map.put("key1","value1");
                map.put("key2","value2");

                String token = builder.setSubject(name)  //主题，就是token中携带的数据
                        .setIssuedAt(new Date())  //设置token是生成时间
                        .setId(one.getUserId() + "")   //设置用户id为tokenid
                        .setClaims(map)            //map中可以存放用户的角色权限信息
                        .setExpiration(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000)) //设置token过期时间
                        .signWith(SignatureAlgorithm.HS256, "woshinendie")  //设置加密的方式和加密密码
                        .compact();

                return new ResultVO(ResStatus.OK,token,one);
            }else {
                return new ResultVO(ResStatus.NO,"登录失败，密码错误",null);
            }
        }
    }
}
